<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: sitemap.php 308 2013-09-24 09:30:12Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 308 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-24 09:30:12 +0000 (Tue, 24 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$module_smarty = new Smarty;
$module_smarty->assign('tpl_path','templates/'.CURRENT_TEMPLATE.'/');

require_once(DIR_FS_INC.'inc.count_products_in_category.php');

function get_category_tree($parent_id = '0', $spacing = '', $exclude = '', $category_tree_array = '', $include_itself = false, $cPath = '') {
	global $db;
	
	if ($parent_id == 0)
		$cPath = ''; 
	else 
		$cPath .= $parent_id.'_';
	
	if(!is_array($category_tree_array)) 
		$category_tree_array = array();
		
	if((sizeof($category_tree_array) < 1) && ($exclude != '0')) 
		$category_tree_array[] = array('id' => '0', 'text' => TEXT_TOP);
	
	if ($include_itself) {
		$category = $db->db_query("SELECT 
										cd.categories_name 
									FROM 
										".TABLE_CATEGORIES_DESCRIPTION." cd 
									WHERE 
										cd.language_id = '".$_SESSION['languages_id']."' 
									AND 
										c.categories_status = '1' 
									AND 
										cd.categories_id = '".$parent_id."'");

		$category_tree_array[] = array('id' => $parent_id, 'text' => $category->fields['categories_name']);
	}
	
	$categories = $db->db_query("SELECT 
									c.categories_id, 
									cd.categories_name, 
									c.parent_id 
								FROM 
									".TABLE_CATEGORIES." c, 
									".TABLE_CATEGORIES_DESCRIPTION." cd 
								WHERE 
									c.categories_id = cd.categories_id 
								AND 
									cd.language_id = '".$_SESSION['languages_id']."' 
								AND 
									c.parent_id = '".$parent_id."' 
								AND 
									c.categories_status = '1' 
								ORDER BY 
									c.sort_order, cd.categories_name");
	
	while(!$categories->EOF) {

		if($exclude != $categories->fields['categories_id']) {
			$category_tree_array[] = array('id' => $categories->fields['categories_id'],
											'text' => $spacing.$categories->fields['categories_name'],
											'link' => href_link(FILENAME_DEFAULT, 'cPath='.$categories->fields['categories_id']));
			$category_tree_array = get_category_tree($categories->fields['categories_id'], $spacing.'&nbsp;&nbsp;&nbsp;', $exclude, $category_tree_array, false, $cPath);
		}
		$categories->MoveNext();
	}
	
	return $category_tree_array;
}
  
if(GROUP_CHECK == 'true')
	$group_check = " AND c.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1";

$categories = $db->db_query("SELECT 
								c.categories_image, 
								c.categories_id, 
								cd.categories_name 
							FROM 
								".TABLE_CATEGORIES." c 
							LEFT JOIN 
								".TABLE_CATEGORIES_DESCRIPTION ." cd 
									ON c.categories_id = cd.categories_id 
							WHERE 
								c.categories_status = '1' 
							AND 
								cd.language_id = ".$_SESSION['languages_id']." 
							AND 
								c.parent_id = '0' ".$group_check." 
							ORDER BY 
								c.sort_order ASC");

$cat_content = array();
while(!$categories->EOF) {
	$cat_content[] = array('ID' => $categories->fields['categories_id'],
							'CAT_NAME' => $categories->fields['categories_name'],
							'CAT_IMAGE' => DIR_WS_IMAGES.'categories/'.$categories->fields['categories_image'],
							'CAT_LINK' => href_link(FILENAME_DEFAULT, 'cPath='.$categories->fields['categories_id']),
							'SCATS' => get_category_tree($categories->fields['categories_id'], '', 0));
	$categories->MoveNext();
}

$categories = $db->db_query("SELECT 
								categories_id, 
								titel
							FROM 
								".TABLE_BLOG_CATEGORIES." 
							WHERE 
								status = 2 
							AND 
								language_id = '".(int)$_SESSION['languages_id']."' 
							ORDER BY 
								position ASC");
								
$blog_content = array();
while(!$categories->EOF){
	$blog_items = $db->db_query("SELECT 
									item_id, 
									name
								FROM 
									".TABLE_BLOG_ITEMS." 
								WHERE 
									status = 2 
								AND 
									language_id = '".(int)$_SESSION['languages_id']."' 
								AND 
									categories_id = '".(int)$categories->fields['categories_id']."' 
								ORDER BY 
									position ASC");

	$blog_sub_content = array();
	while(!$blog_items->EOF){		
		$blog_sub_content[] = array('text' => $blog_items->fields['name'], 
									'link' => href_link(FILENAME_BLOG, 'blog_cat='.$categories->fields['categories_id'].'&blog_item='.$blog_items->fields['item_id']));
		$blog_items->MoveNext();
	}

	$blog_content[] = array('CAT_NAME' => $categories->fields['titel'], 
							'CAT_LINK' => href_link(FILENAME_BLOG, 'blog_cat='.$categories->fields['categories_id']), 
							'SCATS' => $blog_sub_content);
	$categories->MoveNext();
}
	
if(!empty($blog_content))
	$module_content = array_merge($cat_content, $blog_content);
else
	$module_content = $cat_content;

if (sizeof($module_content)>=1) {
	$module_smarty->assign('language', $_SESSION['language']);
	$module_smarty->assign('module_content', $module_content);
	
	if (!CacheCheck()) {
		$module_smarty->caching = false;
		echo $module_smarty->fetch(CURRENT_TEMPLATE.'/module/sitemap.html');
	} else {
		$module_smarty->caching = true;
		$module_smarty->cache_lifetime=CACHE_LIFETIME;
		$module_smarty->cache_modified_check=CACHE_CHECK;
		$cache_id = $GET['cPath'].$_SESSION['language'].$_SESSION['customers_status']['customers_status_name'].$_SESSION['currency'];
		echo $module_smarty->fetch(CURRENT_TEMPLATE.'/module/sitemap.html',$cache_id);
	}
}
?>